package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.*;
import com.csun.cmny.provider.model.dto.service.*;
import com.csun.cmny.provider.model.vo.MerchantCountVO;
import com.csun.cmny.provider.model.vo.OrderCountVO;
import com.csun.cmny.provider.model.vo.ServiceOrderDetailsVo;
import com.csun.cmny.provider.model.vo.ServiceOrderVO;
import com.csun.cmny.provider.model.vo.service.ServiceCountStatusVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcServiceOrderService;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/serviceOrder", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcServiceOrderController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcServiceOrderService omcServiceOrderService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /*
    * 分页查询
    * */
    @ValidateAnnotation
    @PostMapping(value = "/listPage")
    public Wrapper listPage (@RequestBody @Valid ServiceOrderQueryDto serviceOrderQueryDto, BindingResult br) {

        PageInfo pageInfo = omcServiceOrderService.listPage(serviceOrderQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,pageInfo);
    }

    /*
     * 待抢单分页查询
     * */
    @ValidateAnnotation
    @PostMapping(value = "/listOrderPage")
    public Wrapper listPage (@RequestBody @Valid OmcAddressDto omcAddressDto, BindingResult br) {

        omcAddressDto.setUserIdGet(getLoginAuthDto().getUserId());

        PageInfo pageInfo = omcServiceOrderService.listOrderPage(omcAddressDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,pageInfo);
    }

    /*
     * 历史订单分页查询
     * */
    @ValidateAnnotation
    @PostMapping(value = "/listServicedOrder")
    public Wrapper listPage (@RequestBody @Valid OmcServicedOrderDto omcServicedOrderDto, BindingResult br) {

        omcServicedOrderDto.setUserId(getLoginAuthDto().getUserId());
        PageInfo pageInfo = omcServiceOrderService.listServicedOrder(omcServicedOrderDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,pageInfo);
    }

    /*
     * 获取当前服务人员总佣金
     * */
    @PostMapping(value = "/getTotalCommission")
    public Wrapper getTotalCommission (@RequestBody OmcServicedOrderDto omcServicedOrderDto, BindingResult br) {

        omcServicedOrderDto.setUserId(getLoginAuthDto().getUserId());
        Double result = omcServiceOrderService.getTotalCommission(omcServicedOrderDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
    * 下单
    * */
    @ValidateAnnotation
    @PostMapping(value = "/put")
    public Wrapper put (@RequestBody  @Valid ServiceOrderDto serviceOrderDto, BindingResult br) {



        String orderId = omcServiceOrderService.addServiceOrder(serviceOrderDto,getLoginAuthDto());
        /*if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }*/
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,orderId);
    }

    /*
    * 商家确认
    * */

    @PostMapping(value = "/ensure/{orderId}")
    public Wrapper ensure (@PathVariable Long orderId) {

        Integer result = omcServiceOrderService.ensure(orderId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
    * 接单
    * */
    @ValidateAnnotation
    @PostMapping(value = "/get")
    public Wrapper get (@RequestBody @Valid GetOrderDto getOrderDto, BindingResult br) {

        Integer result = omcServiceOrderService.getServiceOrder(getOrderDto,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
    * 指派
    * */
    @ValidateAnnotation
    @PostMapping(value = "/assign")
    public Wrapper assign (@RequestBody @Valid GetOrderByAssignDto getOrderDto, BindingResult br) {

        Integer result = omcServiceOrderService.assignServiceOrder(getOrderDto,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
     * 撤单
     * */
    @PostMapping(value = "/retreat/{orderId}")
    public Wrapper retreat (@PathVariable Long orderId) {

        Integer result = omcServiceOrderService.retreatServiceOrder(orderId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
     * 服务人员撤单
     * */
    @PostMapping(value = "/retreatByTaker/{orderId}")
    public Wrapper retreatByTaker (@PathVariable Long orderId) {

        Integer result = omcServiceOrderService.retreatServiceOrderByTaker(orderId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
    * 完成订单
    * */
    @PostMapping(value = "/complete/{orderId}")
    public Wrapper complete (@PathVariable Long orderId) {

        Integer result = omcServiceOrderService.completeServiceOrder(orderId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    /*
    * 订单详情
    * */
    @PostMapping(value = "/detail/{orderId}")
    public Wrapper detail (@PathVariable Long orderId) {

        ServiceOrderVO vo = omcServiceOrderService.detail(orderId,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,vo);
    }

    /*
     * 订单详情
     * */
    @PostMapping(value = "/orderDetail/{orderId}")
    public Wrapper orderDetail (@PathVariable Long orderId) {

        ServiceOrderDetailsVo vo = omcServiceOrderService.orderDetail(orderId,getLoginAuthDto());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,vo);
    }

    /*
    * 订单统计
    * */
    @ValidateAnnotation
    @PostMapping(value = "/orderCount")
    public Wrapper orderCount (@RequestBody @Valid CountQueryDTO countQueryDTO, BindingResult br) {
        List<OrderCountVO> voList = omcServiceOrderService.orderCount(countQueryDTO);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,voList);
    }

    /*
     * 商家统计
     * */
    @ValidateAnnotation
    @PostMapping(value = "/merchantCount")
    public Wrapper merchantCount (@RequestBody @Valid CountQueryDTO countQueryDTO, BindingResult br) {
        List<MerchantCountVO> voList = omcServiceOrderService.merchantCount(countQueryDTO);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,voList);
    }


    /**
     * @date    2020-08-24
     * @author  chisj
     * @desc    新增接口 待服务列表,到店打卡,到店取餐,送达打卡,
     *
     * 服务完成接口 -- /complete/{orderId}
     *
     */

    /**
     * 待服务列表
     */
    @PostMapping(value = "/showWaitServiceTask")
    public Wrapper showWaitServiceTask(@RequestBody ServiceOrderQueryDto serviceOrderQueryDto) {


        List<ServiceOrderVO> serviceOrderVOList = omcServiceOrderService.showWaitServiceTask(serviceOrderQueryDto,getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),new PageInfo<>(serviceOrderVOList));
    }

    /**
     * 到店打卡
     */
    @ValidateAnnotation
    @PostMapping(value = "/checkInById")
    public Wrapper checkInById(@RequestBody @Valid ServiceOrderOperationDto serviceOrderOperationDto, BindingResult br) {



        Integer result = omcServiceOrderService.checkInById(serviceOrderOperationDto, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }


    /**
     * 服务送达
     */
    @ValidateAnnotation
    @PostMapping(value = "/checkOutById")
    public Wrapper checkOutById(@RequestBody @Valid ServiceOrderOperationDto serviceOrderOperationDto, BindingResult br) {

        Integer result = omcServiceOrderService.checkOutById(serviceOrderOperationDto, getLoginAuthDto());

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

    /**
     * 服务人员实时定位上传
     */
    @ValidateAnnotation
    @PostMapping(value = "/uploadThePositioning")
    public Wrapper uploadThePositioning(@RequestBody @Valid ServiceOrderUploadThePositioningDto serviceOrderUploadThePositioningDto, BindingResult br) {


        redisTemplate.opsForValue().set(RedisKeyUtil.getOmcRealTimeOrderPositioning(getLoginAuthDto().getUserId().toString()),serviceOrderUploadThePositioningDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null));
    }

    /*
     * 三助查询每种订单状态的数量
     * */

    @PostMapping(value = "/selectOrderNumbersByStatus")
    public Wrapper selectOrderNumbersByStatus (@RequestBody ServiceOrderCountDto serviceOrderCountDto) {


        List<ServiceCountStatusVo> serviceCountStatusVos = omcServiceOrderService.selectOrderNumbersByStatus(serviceOrderCountDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),serviceCountStatusVos);
    }

    /*
     * 一护查询每种订单状态的数量
     * */

    @PostMapping(value = "/selectlongRangeOrderNumbersByStatus")
    public Wrapper selectlongRangeOrderNumbersByStatus (@RequestBody LongRangeServiceOrderCountDto longRangeServiceOrderCountDto) {


        List<ServiceCountStatusVo> serviceCountStatusVos = omcServiceOrderService.selectlongRangeOrderNumbersByStatus(longRangeServiceOrderCountDto);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),serviceCountStatusVos);
    }

    /*
     * 订单支付
     * */
    @ValidateAnnotation
    @PostMapping(value = "/pay/{id}")
    public Wrapper pay (@PathVariable Long id) {
        int result=omcServiceOrderService.pay(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }
}


